<template>
  <div>
    <!--表格栏-->
    <el-table :data="data.content" border stripe highlight-current-row @selection-change="selectionChange"
              :v-loading="loading" :max-height="maxHeight" :size="size" :align="align" style="width:100%;">
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column v-for="column in columns"
                       :prop="column.prop" :label="column.label" :width="column.width" :min-width="column.minWidth"
                       :sortable="column.sortable" :fixed="column.fixed" :key="column.prop" :type="column.type">
      </el-table-column>
      <el-table-column label="操作" width="228" fixed="right">
        <template slot-scope="scope">
          <vue-button label="查看" :perms="permsView" :size="size" type="text" @click="handleView(scope.$index, scope.row)"/>
          <vue-button label="编辑" :perms="permsEdit" :size="size" type="text" @click="handleEdit(scope.$index, scope.row)"/>
          <vue-button label="删除" :perms="permsDelete" :size="size" type="text"
                      @click="handleDelete(scope.$index, scope.row)"/>
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <div class="toolbar" style="padding:10px;">
      <vue-button label="批量删除" :perms="permsDelete" :size="size" type="danger" @click="handleBatchDelete()"
                  :disabled="this.selections.length===0" style="float:left;"/>
      <el-pagination layout="total, prev, pager, next, jumper" @current-change="refreshPageRequest"
                     :current-page="pageRequest.pageNum" :page-size="pageRequest.pageSize" :total="data.totalSize"
                     style="float:right;">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import vueButton from "./vueButton"

  export default {
    name: 'vueTable',
    components: {
      vueButton
    },
    props: {
      columns: Array, // 表格列配置
      data: {
        type: Object,
        default: {}
      }, // 表格分页数据
      permsView: String, // 查看权限标识
      permsEdit: String,  // 编辑权限标识
      permsDelete: String,  // 删除权限标识
      size: { // 尺寸样式
        type: String,
        default: 'mini'
      },
      align: {  // 文本对齐方式
        type: String,
        default: 'left'
      },
      maxHeight: {  // 表格最大高度
        type: Number,
        default: 450
      }
    },
    data() {
      return {
        // 分页信息
        pageRequest: {
          pageNum: 1,
          pageSize: 10
        },
        loading: false,  // 加载标识
        selections: []  // 列表选中列
      }
    },
    methods: {
      // 分页查询
      findPage() {
        this.$emit('findPage', this.pageRequest)
      },
      // 选择切换
      selectionChange(selections) {
        this.selections = selections
        this.$emit('selectionChange', selections)
      },
      // 换页刷新
      refreshPageRequest(pageNum) {
        this.pageRequest.pageNum = pageNum
        this.findPage()
      },
      // 查看
      handleView(index, row) {
        this.$emit('handleView', index, row)
      },
      // 编辑
      handleEdit(index, row) {
        this.$emit('handleEdit', {index: index, row: row})
      },
      // 删除
      handleDelete(index, row) {
        let rows = []
        rows.push(row)
        this.delete(rows)
      },
      // 批量删除
      handleBatchDelete() {
        this.delete(this.selections)
      },
      // 删除操作
      delete(rows) {
        this.$emit('handleDelete', rows)
      }
    },
    mounted() {
      this.refreshPageRequest(1)
    }
  }
</script>

<style scoped>

</style>
